import { Sequelize, DataTypes } from 'sequelize';
//import {Comment} from '../db/schema2.js';
const sequelize =  new Sequelize('study', 'root', '1234', {
  host: 'localhost',
  dialect: 'mysql'
});
export const Comment = sequelize.define('Comment', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    comment: '评论ID'
  },
  post_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '关联的帖子ID（外键）',
    references: {
      model: 'posts', // 关联的表名
      key: 'id'       // 关联的字段
    },
    onDelete: 'CASCADE', // 可选：设置外键删除策略
    onUpdate: 'CASCADE'  // 可选：设置外键更新策略
  },
  content: {
    type: DataTypes.TEXT,
    allowNull: false,
    comment: '评论内容'
  },
  user_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '评论用户ID'
  },
  user_name: {
    type: DataTypes.STRING(50),
    allowNull: false,
    comment: '评论用户姓名'
  },
  user_avatar: {
    type: DataTypes.STRING(200),
    allowNull: true,
    defaultValue: null,
    comment: '用户头像'
  },
  parent_id: {
    type: DataTypes.INTEGER,
    allowNull: true,
    defaultValue: null,
    comment: '父评论ID（支持回复）'
  },
  like_count: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
    comment: '点赞数'
  },
  status: {
    type: DataTypes.ENUM('active', 'deleted'),
    allowNull: false,
    defaultValue: 'active',
    comment: '状态'
  },
  created_at: {
    type: DataTypes.DATE,
    allowNull: false,
    defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
    comment: '创建时间'
  },
  updated_at: {
    type: DataTypes.DATE,
    allowNull: false,
    defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
    comment: '更新时间'
  }
}, {
  tableName: 'comments',
  comment: '评论表',
  timestamps: false, // 禁用Sequelize自动生成的createdAt/updatedAt，因为我们已自定义
  indexes: [
    { name: 'idx_post_id', fields: ['post_id'] },
    { name: 'idx_user_id', fields: ['user_id'] },
    { name: 'idx_parent_id', fields: ['parent_id'] },
    { name: 'idx_created_at', fields: ['created_at'] }
  ],
  // 外键约束也可通过associate方法定义（推荐关联模型时使用）
  // 若要在这里显式定义外键约束，需确保posts表已提前定义
});
export const Post = sequelize.define('Post', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    comment: '帖子ID'
  },
  title: {
    type: DataTypes.STRING(200),
    allowNull: false,
    comment: '帖子标题'
  },
  content: {
    type: DataTypes.TEXT,
    allowNull: false,
    comment: '帖子内容'
  },
  author_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '作者ID'
  },
  author_name: {
    type: DataTypes.STRING(50),
    allowNull: false,
    comment: '作者姓名'
  },
  category: {
    type: DataTypes.STRING(50),
    allowNull: true,
    defaultValue: 'general',
    comment: '分类'
  },
  view_count: {
    type: DataTypes.INTEGER,
    allowNull: true,
    defaultValue: 0,
    comment: '浏览数'
  },
  like_count: {
    type: DataTypes.INTEGER,
    allowNull: true,
    defaultValue: 0,
    comment: '点赞数'
  },
  status: {
    type: DataTypes.ENUM('published', 'draft', 'archived'),
    allowNull: false,
    defaultValue: 'published',
    comment: '状态'
  },
  created_at: {
    type: DataTypes.DATE,
    allowNull: false,
    defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
    comment: '创建时间'
  },
  updated_at: {
    type: DataTypes.DATE,
    allowNull: false,
    defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
    comment: '更新时间'
  }
}, {
  tableName: 'posts',
  comment: '帖子表',
  timestamps: false, // 禁用自动生成的createdAt/updatedAt
  indexes: [
    { name: 'idx_author', fields: ['author_id'] },
    { name: 'idx_category', fields: ['category'] },
    { name: 'idx_created_at', fields: ['created_at'] }
  ]
});
// 关键：显式定义 Post 和 Comment 的关联关系
Post.hasMany(Comment, { 
  foreignKey: 'post_id', 
  as: 'comments' 
});
Comment.belongsTo(Post, { 
  foreignKey: 'post_id', 
  as: 'post' 
});
// 正确写法
// const postsWithComments = await Post.findAll({
//   where: { id: 1 },
//   include: [{ model: Comment, as: 'comments' }],
//   raw: true,
// });
// console.log(postsWithComments);


